import router from "../router";
//全局引用store
import store from "../store";
//引入菜单接口

import {
  getleftmenu
} from "../api/user/login";

const routerList = []

export function getChildrenPath(apiUrl) {

  var menu;
  var ids = {
    Id: localStorage.getItem("userId")
  }
  getleftmenu(ids).then(res => {
    res.data.data.forEach(me => {
      setChild(me, routerList, '', '')
    })
    let rou = {
      path: '/index',
      name: 'index',
      component: () => import('@/views/layout/index'),
      children: routerList
    }
    router.addRoute(rou)
  })

}

//递归算法，递归循环: 在方法内部调用方法本身的过程，可以得到一个无限层级的集合对象
function setChild(me, routerList, rootName, rootPath) {
  rootName = rootName + '/' + me.name //   rootName=/系统设置/人员管理
  rootPath = rootPath + '/' + me.path //   rootPath=/sysConfig/..
  if (me.children != null && me.children != [] && me.children.length > 0) {
    //如果有下层
    me.children.forEach(node => {
      setChild(node, routerList, rootName, rootPath)
    })
  }
  //没有下层则说明这是一个路由
  let rou = {
    name: rootName,
    path: rootPath,
    component: () => import('@/views' + rootPath) // 注意字母大小写 @/views/common/BaiduMap
  }
  routerList.push(rou)
}

// //动态获取菜单集合
// 注意：需要重新引入：  import axios from 'axios'
// axios
// .get(apiUrl+'?userId='+localStorage.getItem("userId"))
// .then((response) => {
//   var menus = response.data.Items;
//   menus.forEach((me) => {
//     setChild(me,  routerList, "", "");
//   });
//   let rou = {
//     path: "/index",
//     name: "index",
//     component: () => import("@/views/Main/Index"),
//     children: routerList,
//   };
//   console.info(rou)
//   router.addRoute(rou);
// }); 


